package cn.like.function;


import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 函数
 *
 * @author <a href="mailto:likelovec@gmail.com">like</a>
 * @date 2021/7/17 20:58
 */
public interface Func {

    /**
     * 转换为大写
     */
    Function<String, String> toUpperCase = String::toUpperCase;

    /**
     * 是否为空
     * <pre>
     *     1.null
     *     2.new Object[]()
     *     3.new Collection<>()
     *     4.new Map<>()
     *     5.""
     *     6."    "
     * </pre>
     */
    Predicate<Object> isBlack = (object) -> {
        if (Objects.isNull(object)) { return Boolean.TRUE;}
        if (object instanceof Object[]) return ((Object[]) object).length == 0;
        if (object instanceof Collection) return ((Collection<?>) object).isEmpty();
        if (object instanceof Map) return ((Map<?, ?>) object).isEmpty();
        if (object instanceof CharSequence) {
            final CharSequence str = (CharSequence) object;
            int length;
            int c;
            if ((length = str.length()) == 0) return true;
            for (int i = 0; i < length; i++) {
                c = str.charAt(i);
                if (!(Character.isWhitespace(c)
                        || Character.isSpaceChar(c)
                        || c == '\ufeff'
                        || c == '\u202a'
                        || c == '\u0000')) {
                    return false;
                }
            }
            return true;
        }
        return Boolean.FALSE;
    };

    Predicate<Object> isNotBlack = (o) -> !isBlack.test(o);
}
